package com.baizhi.controller;

//import com.baizhi.dto.CommentDTO;
//import com.baizhi.dto.Reviewer;
import com.baizhi.entity.User;
//import com.baizhi.service.CommentService;
import com.baizhi.service.UserService;
//import com.baizhi.utils.JSONUtils;
import com.baizhi.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserServiceController {

    private static final Logger log = LoggerFactory.getLogger(UserServiceController.class);
    @Autowired
    private UserService userService;
//    private final FavoriteService favoriteService;
    /*@Autowired*/
//    private CommentService commentService;


    /*//根据用户id返回用户信息服务
    @GetMapping("/userInfo/{id}")
    public User user(@PathVariable("id") Integer id) {
        log.info("接收到用户id: {}", id);
        User user = userService.getById(id);
        log.info("返回的用户信息: {}", JSONUtils.writeValueAsString(user));
        return user;
    }*/

    /**
     * 根据用户id视频id查询是否收藏
     *
     * @param videoId
     * @param userId
     * @return
     */
//    @GetMapping("/userInfo/favorite")
//    public Favorite favorite(@RequestParam("videoId") String videoId, @RequestParam("userId") String userId) {
//        log.info("接收到的视频id {}, 用户id: {}", videoId, userId);
//        Favorite favorite = favoriteService.queryByVideoIdAndUserId(Integer.valueOf(videoId), Integer.valueOf(userId));
//        log.info("当前返回的收藏对象是否为空: {}", ObjectUtils.isEmpty(favorite));
//        return favorite;
//    }


    /**
     * 用户发表评论信息
     */
//    @PostMapping("/user/comment/{userId}/{videoId}")
//    public void comments(@PathVariable("userId") Integer userId, @PathVariable("videoId") Integer videoId, @RequestBody Comment comment) {
//        //接受到评论
//        log.info("视频id: {}", videoId);
//        log.info("评论信息: {}", JSONUtils.writeValueAsString(comment));
//        log.info("评论用户信息: {}", userId);
//        //设置评论用户信息
//        comment.setUid(userId);
//        //设置评论视频
//        comment.setVideoId(videoId);
//        commentService.insert(comment);
//    }


    /**
     * 视频评论
     */
    /*@GetMapping("/user/comments")
    public Map<String, Object> comments(@RequestParam("videoId") Integer videoId,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "per_page", defaultValue = "15") Integer rows) {
        log.info("视频id: {}", videoId);
        log.info("当前页: {}", page);
        log.info("每页显示记录数: {}", rows);
        Map<String, Object> result = new HashMap<>();

//        List<CommentDTO> commentVOList = new ArrayList<>();

//        Page<Comment> commentPage = new Page<>(page,rows);
//        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
//        commentQueryWrapper.eq("video_id",videoId);
//        commentService.page(commentPage,commentQueryWrapper);
//
//        List<Comment> comments = commentPage.getRecords();

        //总条数
        Integer count = commentService.findByVideoIdCount(videoId, page, rows);
        result.put("total_count",count);

        List<CommentDTO> byVideoId = commentService.findByVideoId(videoId, page, rows);

        byVideoId.forEach(comment -> {
            Reviewer reviewer = new Reviewer();
            User user = userService.getById(comment.getReviewer().getId());
            BeanUtils.copyProperties(user, comment.getReviewer());
//            commentDTO.setReviewer(reviewer);
//            commentVOList.add(commentDTO);
            List<CommentDTO> subComments = comment.getSubComments();
            if (subComments != null){
                for (CommentDTO subComment : subComments) {
                    if(subComment != null){
                        if(subComment.getReviewer() != null){
                            if(subComment.getReviewer().getId() != null){
                                User user2 = userService.getById(subComment.getReviewer().getId());
                                BeanUtils.copyProperties(user2, subComment.getReviewer());
                            }
                        }
                    }
                }
            }
        });
        result.put("items", byVideoId);
        return result;
    }*/
}